package com.menggudf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.menggudf.entity.SysCaseHistory;

@Repository
public class SysCaseHistoryDao {
    
    private static final Log log = LogFactory.getLog(SysCaseHistoryDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(SysCaseHistory transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(SysCaseHistory persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public SysCaseHistory merge(SysCaseHistory detachedInstance) {
        try {
            SysCaseHistory result = entityManager.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public SysCaseHistory findById(Integer id) {
        try {
            SysCaseHistory instance = entityManager.find(SysCaseHistory.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<SysCaseHistory> getList(String userName, int pageSize, int offset) {
        try {
            StringBuffer sqld = new StringBuffer("from SysCaseHistory s where s.status in (0,1) ");
            if(StringUtils.hasText(userName)){
                sqld.append(" and s.userName=?1 ");
            }
            sqld.append(" order by s.createTime desc ");
            Query query = entityManager.createQuery(sqld.toString()).setFirstResult(offset).setMaxResults(pageSize);
            if(StringUtils.hasText(userName)){
                query.setParameter(1, userName);
            }
            return query.getResultList();
        } catch (Exception e) {
            log.error("getList fail",e);
            throw e;
        }
    }

    public long getListCount(String userName) {
        try {
            StringBuffer sqld = new StringBuffer("select count(*) from SysCaseHistory s where s.status in (0,1) ");
            if(StringUtils.hasText(userName)){
                sqld.append(" and s.userName=?1 ");
            }
            Query query = entityManager.createQuery(sqld.toString());
            if(StringUtils.hasText(userName)){
                query.setParameter(1, userName);
            }
            return (long) query.getSingleResult();
        } catch (Exception e) {
            log.error("getList fail",e);
            throw e;
        }
    }

}
